/* 
@author maodou 
@createTimer 2024/1/17
@desc  axios二次封装
*/
import axios, { AxiosResponse, RawAxiosRequestHeaders } from 'axios'
import { storage } from '@/utils'

import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
const http = axios.create({
    baseURL: process.env.REACT_APP_ENV === 'production' ? process.env.REACT_APP_NETWORK : process.env.REACT_APP_BASEURL,//基本路径,
    timeout: 5000,
})

//拦截器
http.interceptors.request.use((config) => {

    let token: string = storage("Access-Token");
    if (token) {
        config.headers["Access-Token"] = token;
    }
    NProgress.start();
    return config;
}, (error) => {
    return Promise.reject(error);
})

http.interceptors.response.use((response) => {
    NProgress.done();
    return Promise.resolve(response);
}, (error) => {
    return Promise.reject(error);
})

//get  post

export function get<T = any, D = any>(url: string, params?: D, headers?: RawAxiosRequestHeaders): Promise<AxiosResponse<T>> {
    return http.get<T, AxiosResponse<T>, D>(url, {
        params: {
            ...params
        },
        headers: headers
    })
}



export const post = <T = any, D = any>(url: string, data?: D, headers?: RawAxiosRequestHeaders): Promise<AxiosResponse<T>> => {
    return http.post<T, AxiosResponse<T>, D>(url, data, {
        headers: headers
    })
}